package com.shareach.service.impl;  

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import com.shareach.common.BaseQueryOrder;
import com.shareach.dao.mongo.BaseSiteDao;
import com.shareach.domain.BaseSiteDomain;
import com.shareach.service.BaseSiteService;
import com.shareach.service.exception.BaseService;
import com.shareach.util.ReflectionUtil;

/** 
 * create time：2013-1-30 下午6:41:22 
 * @author retryu 
 * @since JDK 1.6
 * @version 1.0  
 * description： 
 */
@Service
public class BaseSiteServiceImpl extends BaseService<BaseSiteDomain> implements BaseSiteService {

	
	@Autowired
	private  BaseSiteDao baseSiteDao;

	@Override
	public Long save(BaseSiteDomain domain) {
		return baseSiteDao.insert(domain);
	}

	@Override
	public void del(BaseSiteDomain domain) {
		if(domain.getId()==null)return ;
		Query  query=new  Query();
		query.addCriteria(new  Criteria("_id").is(domain.getId()));
		baseSiteDao.delete(query, BaseSiteDomain.class);
	}

	@Override
	public void update(BaseSiteDomain domain) {
		
	}

	@Override
	public BaseSiteDomain getById(Long id) {
		if(id==null)return null;
		Query query=new  Query();
		query.addCriteria(Criteria.where("_id").is(id));
		BaseSiteDomain  baseSiteDomain=baseSiteDao.findOne(query);
		return  baseSiteDomain;
	}

	@Override
	public List<BaseSiteDomain> getList(BaseSiteDomain domain) {
		Query  query=new  Query();
		Map<Object, Object>  domainMap=ReflectionUtil.parseDomaintoMap(domain);
		for (Map.Entry<Object, Object> entry : domainMap.entrySet()) {
			query.addCriteria(Criteria.where(entry.getKey().toString()).is(entry.getValue()));
		}
		List<BaseSiteDomain>  baseSiteDomains=baseSiteDao.findList(query);
		return  baseSiteDomains;
	}

	@Override
	public Page<BaseSiteDomain> getPageResult(BaseSiteDomain domain, BaseQueryOrder order) {
		Query query = parseDomaintoQuery(domain);
		Page<BaseSiteDomain> domainPage = baseSiteDao.getPageResult(query, order);
		return domainPage;
	}
}
  
